﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for CategoryAdapter
/// </summary>
public class CategoryAdapter
{
	public CategoryAdapter()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public Dictionary<string, string> getCatagories()
    {
        List<CategoryEntity> categories = new List<CategoryEntity>();
        BookContextDataContext bookContext = new BookContextDataContext();

        try
        {
            List<Category> cateEntities = bookContext.Categories.ToList();
            Dictionary<string, string> dirCategories = new Dictionary<string, string>();
            for (int i = 0; i < cateEntities.Count(); i++)
            {
                dirCategories.Add(cateEntities.ElementAt(i).ID.ToString(), cateEntities.ElementAt(i).Name);
            }

            return dirCategories;
        }
        catch
        {
            return null;
        }
    }

    public List<CategoryEntity> getListCategories()
    {
        List<CategoryEntity> categories = new List<CategoryEntity>();
        BookContextDataContext bookContext = new BookContextDataContext();

        try
        {
            List<Category> list = bookContext.Categories.ToList();

            foreach (Category cate in list)
              {
                  categories.Add(new CategoryEntity(cate));

               }
            return categories;
        }
        catch
        {
            return null;
        }
    }

    public List<CategoryEntity> getListCategoriesByPlatform(long platformID)
    {
        List<CategoryEntity> categories = new List<CategoryEntity>();
        BookContextDataContext bookContext = new BookContextDataContext();

        try
        {
            var list = from c in bookContext.Categories
                                  join d in bookContext.CategoryDetails on c.ID equals d.CategoryID
                                  where d.PlatformID == platformID
                                  select c;

            foreach (Category cate in list)
            {
                categories.Add(new CategoryEntity(cate,platformID));

            }
            return categories;
        }
        catch
        {
            return null;
        }
    }

    public List<CategoryEntity> getCategory(long categoryID)
    {

        List<CategoryEntity> categories = new List<CategoryEntity>();
        BookContextDataContext bookContext = new BookContextDataContext();

        try
        {
            List<Category> list = bookContext.Categories.Where(c => c.ID == categoryID).ToList();

            foreach (Category cate in list)
            {
                categories.Add(new CategoryEntity(cate));

            }
            return categories;
        }
        catch
        {
            return null;
        }
    }

    /// <summary>
    /// Create new Category
    /// </summary>
    /// <param name="newCategory"></param>
    /// <returns></returns> 
    public long createCategory(CategoryEntity newCategory)
    {
        BookContextDataContext bookContext = new BookContextDataContext();
        try
        {
            //Create category
            Category cate = new Category();
            cate.Name = newCategory.Name;
            cate.Description = newCategory.Description;

            bookContext.Categories.InsertOnSubmit(cate);
            bookContext.SubmitChanges();

            // Create categoryDetail
            foreach (CategoryDetailEntity cateDtail in newCategory.categoryDetails)
            {
                bookContext = new BookContextDataContext();
                CategoryDetail tmpCategoryDetail = new CategoryDetail();
                tmpCategoryDetail.CategoryID = cate.ID;
                tmpCategoryDetail.PlatformID = cateDtail.PlatformID;

                bookContext.CategoryDetails.InsertOnSubmit(tmpCategoryDetail);
                bookContext.SubmitChanges();

            }

            return cate.ID;
        }
        catch
        {
            return 0;
        }
    }

    /// <summary>
    /// Update category
    /// </summary>
    /// <param name="newCategory"></param>
    /// <returns></returns> 
    public long UpdateItem(CategoryEntity newCategory)
    {
        BookContextDataContext context = new BookContextDataContext();
        try
        {
            Category category = context.Categories.SingleOrDefault(c => c.ID == newCategory.ID);
            if (category != null)
            {
                // Update Category
                category.Name = newCategory.Name;
                category.Description = newCategory.Description;   
                context.SubmitChanges();

                //Updat category detail
                foreach (CategoryDetailEntity categorydetail in newCategory.categoryDetails)
                {
                    context = new BookContextDataContext();

                    CategoryDetail catedetail =
                        context.CategoryDetails.Where(c => c.CategoryID == categorydetail.CategoryID && c.PlatformID == categorydetail.PlatformID)
                            .FirstOrDefault();

                    if (catedetail == null)
                    {
                        //Update categoryDetail 
                        catedetail = new CategoryDetail();
                        catedetail.CategoryID = categorydetail.CategoryID;
                        catedetail.PlatformID = categorydetail.PlatformID;
                        context.CategoryDetails.InsertOnSubmit(catedetail);
                        context.SubmitChanges();
                    }
                }
         

                List<CategoryDetail> list = context.CategoryDetails.Where(cd => cd.CategoryID == newCategory.ID).ToList();
                foreach (CategoryDetail cateDetail in list)
                {

                    CategoryDetailEntity cateDetailEntity = newCategory.categoryDetails.Where(c => c.CategoryID == cateDetail.CategoryID && c.PlatformID == cateDetail.PlatformID).
                            FirstOrDefault();
                    if (cateDetailEntity == null)
                    {
                        
                        context.CategoryDetails.DeleteOnSubmit(cateDetail);
                        context.SubmitChanges();
                    }
                }

                return category.ID;
            }

        }
        catch
        {
        }
        return 0;
    }

    /// <summary>
    /// Delete category by categoryID
    /// </summary>
    /// <param name="categoryID"></param>
    /// <returns></returns> 
    public bool DeleteCategory(long categoryID)
    {
        BookContextDataContext context = new BookContextDataContext();
        try
        {
            //Delete categorydetail
            List<CategoryDetail> listContent = context.CategoryDetails.Where(c => c.CategoryID == categoryID).ToList();
            context.CategoryDetails.DeleteAllOnSubmit(listContent);

            //Delete category
            Category category = context.Categories.SingleOrDefault(c => c.ID == categoryID);
            context.Categories.DeleteOnSubmit(category);
            context.SubmitChanges();

            return true;
        }
        catch { }
        return false;
    }

    /// <summary>
    /// Search categories by key
    /// </summary>
    /// <param name="categoryID"></param>
    /// <returns></returns> 
    public List<CategoryEntity> searchCategories(string key)
    {

        List<CategoryEntity> categories = new List<CategoryEntity>();
        BookContextDataContext bookContext = new BookContextDataContext();

        try
        {           
            var list = from c in bookContext.Categories
                       where c.Name.Contains(key)
                       select c;
            foreach (Category cate in list)
            {
                categories.Add(new CategoryEntity(cate));
            }     
          
            return categories;
        }
        catch
        {
            return null;
        }
    }
    /* Process for web servive*/
    public Dictionary<string, string> GetCatagoryAll()
    {
        var categories = new Dictionary<string, string>();
        var bookContext = new BookContextDataContext();

        try
        {
            List<Category> cateEntities = bookContext.Categories.ToList();
            foreach (var cate in cateEntities)
            {
                categories.Add(cate.ID.ToString(""), cate.Name);
            }

            return categories;
        }
        catch
        {
            return null;
        }
    }
}